/*
Flexperimentos.
Copyright (C) 2007  Richard R Manzke

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package flexperimentos.formatters
{
	import mx.formatters.Formatter;

	/**
	 * CNPJFormatter
	 * @author Richard R Manzke <tuco.net@gmail.com>
	 * @langversion ActionScript 3.0
	 */
	public class CNPJFormatter extends Formatter
	{
		private const CNPJ_LENGTH:int = 14;
		
		public function CNPJFormatter()
		{
			//TODO: implement function
			super();
		}
		
		override public function format(value:Object):String{
			// retorna Vazio se não existe valor de entrada.
			if (!value)
				return "";
				
			var strValue:String = value.toString();
			// Retorna Vazio se o valor de entrada com tamanho incorreto.
			if (strValue.length != CNPJ_LENGTH) 
				return "";
				
			// Retorna a entrada formatada.
			return strValue.substr(0,2) + "." + 
					strValue.substr(2,3) + "." +
					strValue.substr(5,3) + "/" +
					strValue.substr(8,4) + "-" +
					strValue.substr(12,2);
		}
	}
}